package com.chats.controller;


import com.chats.common.Result;
import com.chats.pojo.User;
import com.chats.service.UserService;
import com.chats.websocket.WebSocketServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {


    @Autowired
    private UserService userService;

    @PostMapping("/register")
    public Result register(@RequestBody User user) {
        log.info("用户注册 username={}", user.getUsername());
        return userService.register(user);
    }

    /**
     * 用户登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        log.info("用户登录 user={}", user.getUsername());
        return userService.login(user);
    }

    /**
     * 判断用户id是否存于聊天室
     * @param id 用户名
     * @return json
     */
    @GetMapping("/exist/{id}")
    public Result isExist(@PathVariable Long id) {
        log.info("用户id={}是否存在于聊天室", id);
        boolean isExist = WebSocketServer.sessionMap.containsKey(id);
        return Result.success(isExist);
    }


}
